package com.hollycrm.hollysqm.util;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;


/**
 * 设置月度或天的顺延方法
 * 注：代码写的很丑，不忍直视（'_'）
 * @author jianglong
 * @date 2017年5月11日 上午11:42:17
 */
public class CalendarUtil {

	/**
	 * 判断两个时间跨月周期，并顺延生成一个新的日期
	 * @param sTime
	 * @param eTime
	 * @param patterm
	 * @throws Exception
	 */
	public static String[] monthTimeMove(String sTime,String eTime,String patterm) throws Exception {
		Date sDate = DateUtils.parseDate(sTime, new String[]{patterm});
		Date eDate = DateUtils.parseDate(eTime, new String[]{patterm});
		
		Calendar rightNow1 = Calendar.getInstance();
		rightNow1.setTime(sDate);
		int year1 = rightNow1.get(Calendar.YEAR);
		int month1 = rightNow1.get(Calendar.MONTH) + 1;
		int day1 = rightNow1.get(Calendar.DAY_OF_MONTH);
		
		Calendar rightNow2 = Calendar.getInstance();
		rightNow2.setTime(eDate);
		int year2 = rightNow2.get(Calendar.YEAR);
		int month2 = rightNow2.get(Calendar.MONTH) + 1;
		int day2 = rightNow2.get(Calendar.DAY_OF_MONTH);
		int ym = 0 ;
		int m = 0;
		if (year1!=year2){
			int y = year2 - year1;			
//			System.out.println("跨年:"+y);
			ym = (y>1?(y-1)*12:0);	
			if (month1!=month2){
				m =  (12 - month1) +  month2 + ym;
			}else {
				m = month2 - month1;
			}
		}else {
			m = month2 - month1;
		}		
		
//		System.out.println("相差月："+m);
		//按月数据顺延
		sDate = DateUtils.addMonths(sDate, m==0?1:m);
		eDate = DateUtils.addMonths(eDate, m==0?1:m);
		//表示是否月末最后一天
		if (day1 == 1 && 
				day2 == rightNow2.getActualMaximum(Calendar.DAY_OF_MONTH)){//设置月末最后一天
			rightNow2.setTime(eDate);
			day2 = rightNow2.getActualMaximum(Calendar.DAY_OF_MONTH);
			rightNow2.set(Calendar.DAY_OF_MONTH, day2);
			eDate = rightNow2.getTime();
		}
		String sd = DateFormatUtils.format(sDate, patterm);
		String ed = DateFormatUtils.format(eDate, patterm);
//		System.out.println("月度顺延，下一个时间周期："+sd+"\t"+ed);
		return new String[]{sd,ed};
	}
	
	/**
	 * 按天数顺延
	 * @param sTime
	 * @param eTime
	 * @param patterm
	 * @return
	 * @throws Exception
	 */
	public static String[] dayTimeMove(String sTime,String eTime,String patterm) throws Exception {
		Date sDate = DateUtils.parseDate(sTime, new String[]{patterm});
		Date eDate = DateUtils.parseDate(eTime, new String[]{patterm});		
		long sl = sDate.getTime();
		long el = eDate.getTime();
		long day = (el-sl)/(1000*60*60*24) + 1;//如果是当天默认往后移一天；
//		System.out.println("相差天数："+(el-sl)/(1000*60*60*24));
		//按天数据顺延
		sDate = DateUtils.addDays(eDate, 1);
		eDate = DateUtils.addDays(eDate, Integer.parseInt(String.valueOf(day)));
		String sd = DateFormatUtils.format(sDate, patterm);
		String ed = DateFormatUtils.format(eDate, patterm);
//		System.out.println("天数顺延，下一个时间周期："+sd+"\t"+ed);
		return new String[]{sd,ed};
	}
	
	
	public static void main(String[] args) throws Exception {
//		CalendarUtil.monthTimeMove("2016-05-08", "2016-11-30", "yyyy-MM-dd");
//		CalendarUtil.monthTimeMove("2016-01-01", "2016-01-31", "yyyy-MM-dd");
//		CalendarUtil.monthTimeMove("2016-02-01", "2016-02-29", "yyyy-MM-dd");
		CalendarUtil.monthTimeMove("2016-12-29", "2017-01-31", "yyyy-MM-dd");
//		CalendarUtil.dayTimeMove("20160508", "20161130", "yyyyMMdd");
//		CalendarUtil.dayTimeMove("20160701", "20160731", "yyyyMMdd");
		
		Date sDate = DateUtils.parseDate("2017-01-31", new String[]{"yyyy-MM-dd"});
		sDate = DateUtils.addMonths(sDate, 1);
		String sd = DateFormatUtils.format(sDate, "yyyy-MM-dd");
		System.out.println(sd);
	}
}
